package com.laiyima.zhongjiang.linghuilv.demo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.laiyima.zhongjiang.linghuilv.demo.util.PhoneCode;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class CaptchaActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImageView;
    private TextView mTextView,ca_number,tv_code6;
    PhoneCode pc_1;
    private EditText editText;
    private Button mButton;
    private CheckBox mCheckBox;
    private String mobile,password,captcha;
    private String TAG="CaptchaActivity";
    private int Captchacode;
    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_captcha);
        mImageView=findViewById(R.id.ca_back);
        mTextView=findViewById(R.id.ca_number);
        mButton=findViewById(R.id.cap_button);


        pc_1=findViewById(R.id.ca_PhoneCode);
        mTextView.setText(mobile);
        mImageView.setOnClickListener(this);
        mButton.setOnClickListener(this);
        ca_number=findViewById(R.id.ca_number);
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String mobile = sharedPreferences.getString("mobile", "");
        ca_number.setText("验证码已发送到"+mobile);



        tv_code6=findViewById(R.id.tv_code6);
        tv_code6.addTextChangedListener(new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 String v=tv_code6.getText().toString();
                 if(v.length()>0){
                     mButton.setEnabled(true);
                     mButton.setTextColor(Color.WHITE);
                     mButton.setBackgroundResource(R.drawable.begin_textview_shape_change);
                 }else {
                     mButton.setTextColor(Color.parseColor("#B8B8B8"));
                     mButton.setEnabled(false);
                     mButton.setBackgroundResource(R.drawable.firist_edittext_shape);
                 }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        setStatusBar();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.ca_back:
                finish();
                break;
            case R.id.cap_button:
                SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                String mobile = sharedPreferences.getString("mobile", "");
                String code = pc_1.getPhoneCode();
                RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/register/code");

                //需要带入的参数
                params.addBodyParameter("mobile", mobile);
                params.addBodyParameter("code", code);
               // params.addBodyParameter("captcha", captcha);
                x.http().post(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.e(TAG, "onSuccess:"+result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            Captchacode = jsonObject.optInt("code", 0);
                            Log.e(TAG, "Captchacode:"+Captchacode);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (Captchacode == 200) {
                            SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                            String mobile = sharedPreferences.getString("mobile", "");
                            String name = sharedPreferences.getString("name", "");
                            String password = sharedPreferences.getString("password", "");
                            RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/register/register");
                            //需要带入的参数
                            params.addBodyParameter("mobile", mobile);
                            params.addBodyParameter("password", password);
                            params.addBodyParameter("name", name);
                            // params.addBodyParameter("captcha", captcha);
                            x.http().post(params, new Callback.CommonCallback<String>() {
                                @Override
                                public void onSuccess(String result) {
                                    Log.e(TAG, "onSuccess:"+result);
                                    try {
                                        JSONObject jsonObject = new JSONObject(result);
                                        Captchacode = jsonObject.optInt("code", 0);
                                        Log.e(TAG, "Captchacode:"+Captchacode);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    if (Captchacode == 200) {
                                        Toast.makeText(getBaseContext().getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                                        Intent userintent=new Intent(CaptchaActivity.this,FirstActivity.class);
                                        userintent.putExtra("mobile", mobile);
                                        userintent.putExtra("password", password);
                                        startActivity(userintent);
                                    }
                                    if (Captchacode == 301) {
                                        Toast.makeText(getBaseContext().getApplicationContext(), "手机号以注册", Toast.LENGTH_SHORT).show();
                                    }else
                                    {
                                    }
                                }
                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {
                                }
                                @Override
                                public void onCancelled(CancelledException cex) {
                                }
                                @Override
                                public void onFinished() {

                                }
                            });

                         }
                        if (Captchacode == 500) {
                            Toast.makeText(getBaseContext().getApplicationContext(), "验证码错误，请输入有效的验证码", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                });
                break;
        }
    }
}




